#include "CommCommand.h"

CommCommand::CommCommand()
{
}


CommCommand& CommCommand::I(){
    static CommCommand instance;
    return instance;
}

//实验动作(62 00 00 3A)
//连接
QString CommCommand::connectToLc()
{
    return "62 00 00 3A 00 06 00 00 E7 2F 55 AA";
}

//启动
QString CommCommand::startExperiment()
{
    return  "62 00 00 3A 00 06 01 00 E7 2F 55 AA";
}

//暂停
QString CommCommand::pauseExperiment()
{
    return "62 00 00 3A 00 06 02 00 E7 2F 55 AA";
}

//继续
QString CommCommand::continueExperiment()
{
    return "62 00 00 3A 00 06 03 00 E7 2F 55 AA";
}

//停止
QString CommCommand::stopExperiment()
{
    return "62 00 00 3A 00 06 04 00 E7 2F 55 AA";
}

//获取机器状态
QString CommCommand::getMachineStatus()
{
    return "62 00 00 3A 00 06 05 00 E7 2F 55 AA";
}

//实验操作(62 00 00 3B)

//获取注射器指令 默认发送
QString CommCommand::getSyring(QString serial,QString number,QString actType,QString step,QString allStep)
{
    QString data;
    //序号
    data+= common->TenToSixteen(serial.toInt(),2)+" ";

    //类型注射器01
    data+="01 ";

    //注射器编号
    data+= common->TenToSixteen(number.toInt(),2)+" ";

    //动作类型(01 吸 02 打)
    if(actType=="吸液"){
        data +="01 ";
    }
    else if(actType=="打液"){
        data +="02 ";
    }

    // 速度(2字节 100)
    data +=common->TenToSixteen(qRound(step.toDouble()),4)+" ";


    //体积(2字节 100)
    data +=common->TenToSixteen(qRound(allStep.toDouble()),8);

    return QString("62 00 00 3B 00 0E %1 FB 53 55 AA").arg(data);
}

//获取阀门指令 默认发送
QString CommCommand::getValve(QString serial,QString type ,QString List)
{
    QString data;
    //序号
    data+= common->TenToSixteen(serial.toInt(),2)+" ";

    //类型 阀门02
    data+= "02 ";

    if(type=="开启"){
        QStringList openList=List.split(",");
        for(int m=1;m<=3;m++){
            QString binaryInfo;
            for(int n=8*m;n>=1+8*(m-1);n--){
                if(openList.contains(QString::number(n)))
                {
                    binaryInfo += "0";
                }
                else
                {
                    binaryInfo += "1";
                }
            }
            data+=common->binaryToHex(binaryInfo)+" ";
        }
    }
    else if(type=="关闭"){
        QStringList closeList=List.split(",");
        for(int m=1;m<=3;m++){
            QString binaryInfo;
            for(int n=8*m;n>=1+8*(m-1);n--){
                if(closeList.contains(QString::number(n)))
                {
                    binaryInfo += "1";
                }
                else
                {
                    binaryInfo += "0";
                }
            }
            data+=common->binaryToHex(binaryInfo)+" ";
        }
    }
    return QString("62 00 00 3B 00 09 %1FB 53 55 AA").arg(data);
}

//获取转盘指令 默认发送
QString CommCommand::getTurntable(QString serial,QString pos)
{
    QString data;
    //序号
    data+= common->TenToSixteen(serial.toInt(),2)+" ";

    data+= "03 ";

    data += common->TenToSixteen(pos.toInt(),2);

    return QString("62 00 00 3B 00 07 %1 FB 53 55 AA").arg(data);
}

//获取盖盖指令 默认发送
QString CommCommand::coverTheLid(QString serial)
{
    return QString("62 00 00 3B 00 07 %1 04 01 FB 53 55 AA").arg(common->TenToSixteen(serial.toInt(),2));
}

//维护操作(62 00 00 3C)

//电机复位到实验位置

QString CommCommand::AirPumpPressure(QString type,QString open,QString close){
    QString openValue=common->TenToSixteen(open.toInt(),4);
    QString closeValue=common->TenToSixteen(close.toInt(),4);
    QString typeValue=common->TenToSixteen(type.toInt(),2);

    return QString("62 00 00 3C 00 0A 02 %1 %2 %3 FB 53 55 AA").arg(typeValue,openValue,closeValue);
}

QString CommCommand::resetExpPos(QString type,QString serial){
    QString code=common->TenToSixteen(type.toInt(),4);
    serial=common->TenToSixteen(serial.toInt(),2);
    QString result=QString("62 00 00 3C 00 08 04 %1 %2 E7 2F 55 AA").arg(code,serial);
    return result;
}

QString CommCommand::resetInitialPosition(QString serial){
    serial=common->TenToSixteen(serial.toInt(),2);
    QString result=QString("62 00 00 3C 00 06 05 %1 E7 2F 55 AA").arg(serial);
    return result;
}

QString CommCommand::singleStop(QString serial){
    serial=common->TenToSixteen(serial.toInt(),2);
    QString result=QString("62 00 00 3C 00 06 0C %1 E7 2F 55 AA").arg(serial);
    return result;
}
